<?php
namespace app\backstage\logic;
use think\Db;
class CostReport
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'cost_date,hour,money';

        $groupByArr = [];
        $type = 1;

        $params['channel_id'] = getChannelId($params,request()->_role->alias,request()->_user->id);

        $condition[] = ['advert_media_id','eq','gt:0','advert_media_id'];
        $condition[] = ['advert_account_id','eq','gt:0','advert_account_id'];
        $condition[] = ['channel_id', 'in', 'not_empty','channel_id'];

        $where   = buildQueryCondition($condition, $params);

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['id' => 'DESC'];
        }

        if(isset($params['channel_id']) && $params['channel_id'] > 0)
        {
            $groupByArr[] = 'channel_id';
        }
        if(isset($params['advert_media_id']) && $params['advert_media_id'] > 0)
        {
            $groupByArr[] = 'advert_media_id';
        }
        if(isset($params['advert_account_id']) && $params['advert_account_id'] > 0)
        {
            $groupByArr[] = 'advert_account_id';
        }

        $groupBy = implode(',',$groupByArr);

        if($groupByArr)
        {
            $groupBy .= ',cost_date,hour';
            $field = 'cost_date,hour,SUM(money) AS money';
        }

        $CostReport = \think\facade\App::model('CostReport');
        $list = $CostReport->getList($type, $where, $field, $params['page'], $params['limit'], $orderBy, $groupBy);

        if ($list) {

            foreach ($list as $k => $v) {
                $list[$k]['money'] = $v['money'] > 0 ? round($v['money']/100,2) : 0;
            }
        }

        $result['count'] = $CostReport->getTotalNumber($type, $where, $groupBy);
        $result['list']  = $list;

        return $result;
    }

    //筛选列表数据
    public function getListScreen($params)
    {
        $today_yet = ['00','01','02','03','04','05','06','07','08','09',
            '10','11','12','13','14','15','16','17','18','19',
            '20','21','22','23',
        ];

        $start_date = date('Y-m-d',strtotime('-6days'));
        $end_date = date('Y-m-d');
        $dates = getDateByRange($start_date,$end_date);

        $params['channel_id'] = getChannelId($params,request()->_role->alias,request()->_user->id);

        $condition[] = ['channel_id', 'in', 'not_empty','channel_id'];
        $condition[] = ['advert_media_id','eq','gt:0','advert_media_id'];
        $condition[] = ['advert_account_id','eq','gt:0','advert_account_id'];

        $where   = buildQueryCondition($condition, $params);

        if($params['type'] == 'week')
        {
            $cost_trend_x = $dates;
            $where[]  = ['cost_date','>=',$start_date];
            $where[]  = ['cost_date','<=',$end_date];

            $field = 'SUM(money) AS money';
            $orderBy = ['cost_date' => 'ASC'];
            $groupBy = 'cost_date';
        }else{
            $cost_trend_x = $today_yet;
            if($params['type'] == 'yesterday')
            {
                $where[]  = ['cost_date','=',date('Y-m-d',strtotime('-1days'))];
            }else{
                $where[]  = ['cost_date','=',date('Y-m-d')];
            }

            $field = 'money';
            $orderBy = ['hour' => 'ASC'];
            $groupBy = '';
        }

        $empty_cost_trend_y = [];
        foreach ($cost_trend_x as $v)
        {
            $empty_cost_trend_y[] = 0;
        }


        $CostReport = \think\facade\App::model('CostReport');
        $list = $CostReport->getList(1, $where, $field, 1, 100, $orderBy, $groupBy);

        if($list)
        {
            $cost_trend_y = $list;
        }else{
            $cost_trend_y = $empty_cost_trend_y;
        }

        return ['data'=>['cost_trend_x'=>$cost_trend_x,'cost_trend_y'=>$cost_trend_y]];
    }
}